package com.fifth.emp.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.EmpInfo;

import com.fifth.common.pojo.entity.Role;
import com.fifth.common.pojo.entity.UserInfo;
import com.fifth.emp.config.WebUtils;
import com.fifth.emp.feign.SsoFeign;
import com.fifth.emp.mapper.EmpMapper;
import com.fifth.emp.service.EmpService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @作者：徐志豪
 * @创建时间 2022/8/31 11:45
 **/
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private SsoFeign ssoFeign;
    /**
     * 根据tel查询员工信息
     * @param telephone
     * @return
     */
    @Override
    public EmpInfo findByPhone(String telephone) {
        QueryWrapper<EmpInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("telephone",telephone);
        return empMapper.selectOne(queryWrapper);
    }

    /**
     * 根据员工名字查询员工信息
     * @param ename
     * @return
     */
    @Override
    public CommonResult findByName(String ename) {
        QueryWrapper<EmpInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("ename",ename);
        EmpInfo info = empMapper.selectOne(wrapper);
        if (info!=null){
            return new CommonResult(2000,"查询成功",info);
        }else {
            return new CommonResult(5000,"查询失败",null);
        }

    }

    /**
     * 添加员工信息
     * @param empInfo
     * @return
     */
    @Override
    public CommonResult insertEmp(EmpInfo empInfo) {

        int row = empMapper.insert(empInfo);
        if (row!=0){
            return new CommonResult(2000,"添加成功",row);
        }else {
            return new CommonResult(5000,"添加失败",null);
        }
    }

    /**
     * 根据id删除员工信息
     * @param id
     * @return
     */
    @Override
    public CommonResult deleteById(String id) {
        int state = empMapper.updateState(id);

        int row = empMapper.deleteById(id);
        if (row!=0){
            return new CommonResult(2000,"删除成功",row);
        }else {
            return new CommonResult(5000,"删除失败",null);
        }
    }

    /**
     * 根据id修改员工信息
     * @param empInfo
     * @return
     */
    @Override
    public CommonResult updateEmp(EmpInfo empInfo) {
        System.out.println("======================"+empInfo);
        int row = empMapper.updateById(empInfo);
        if (row!=0){
            return new CommonResult(2000,"修改成功",row);
        }else {
            return new CommonResult(5000,"修改失败",null);
        }

    }

    @Override
    public CommonResult selectAll(Integer currentPage,Integer pageSize,EmpInfo empInfo) {
        IPage<EmpInfo> page = new Page<>(currentPage,pageSize);
        QueryWrapper<EmpInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("r.is_deleted",0);
        if (StringUtils.hasText(empInfo.getEname())){
            wrapper.like("ename",empInfo.getEname());
        }

        IPage<EmpInfo> page1 = empMapper.selectAll(page,wrapper);
        System.out.println("总页码:" + page.getPages());
        System.out.println("总条数:" + page.getTotal());
        System.out.println("当前页记录:" + page.getRecords());
        return new CommonResult(2000,"cha",page1);
    }

    @Override
    public CommonResult updatePassword(String oldPassword, String newPassword) {
        //获取userid
        HttpServletRequest request = WebUtils.getRequest();
        //获取token
        String token = request.getHeader("token");
        //根据token获取userid
        Map userMap = JSON.parseObject(redisTemplate.opsForValue().get(token), Map.class);
        String userid;
        String salt;
        String password;
        //判读是否登录
        if (userMap!=null && userMap.containsKey("user")) {
            JSONObject user = (JSONObject) userMap.get("user");
            userid = user.getString("id");
            salt = user.getString("salt");
            password = user.getString("password");
        }else {
            return new CommonResult(4000,"登录失效,请登录后再发布",null);
        }
        //判断旧密码输入是否相同
        String s = ssoFeign.signInsert(oldPassword, salt);
        if (s.equals(password)){
            String s1 = ssoFeign.signInsert(newPassword, salt);

            QueryWrapper<EmpInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("id",userid);
            EmpInfo empInfo = new EmpInfo();
            empInfo.setPassword(s1);
            int update = empMapper.update(empInfo, wrapper);
            return new CommonResult(2000,"修改密码成功,请重新登录",update);
        }
        return new CommonResult(5000,"输入密码错误",null);

    }
}
